package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.BizCheckDetail;
import com.ruoyi.system.mapper.BizCheckTitleMapper;
import com.ruoyi.system.domain.BizCheckTitle;
import com.ruoyi.system.service.IBizCheckTitleService;
import com.ruoyi.common.core.text.Convert;

/**
 * 检查清单Service业务层处理
 * 
 * @author gaofeng
 * @date 2025-07-08
 */
@Service
public class BizCheckTitleServiceImpl implements IBizCheckTitleService 
{
    @Autowired
    private BizCheckTitleMapper bizCheckTitleMapper;

    /**
     * 查询检查清单
     * 
     * @param id 检查清单主键
     * @return 检查清单
     */
    @Override
    public BizCheckTitle selectBizCheckTitleById(Long id)
    {
        return bizCheckTitleMapper.selectBizCheckTitleById(id);
    }

    /**
     * 查询检查清单列表
     * 
     * @param bizCheckTitle 检查清单
     * @return 检查清单
     */
    @Override
    public List<BizCheckTitle> selectBizCheckTitleList(BizCheckTitle bizCheckTitle)
    {
        return bizCheckTitleMapper.selectBizCheckTitleList(bizCheckTitle);
    }

    /**
     * 新增检查清单
     * 
     * @param bizCheckTitle 检查清单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBizCheckTitle(BizCheckTitle bizCheckTitle)
    {
        int rows = bizCheckTitleMapper.insertBizCheckTitle(bizCheckTitle);
        insertBizCheckDetail(bizCheckTitle);
        return rows;
    }

    /**
     * 修改检查清单
     * 
     * @param bizCheckTitle 检查清单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBizCheckTitle(BizCheckTitle bizCheckTitle)
    {
        bizCheckTitleMapper.deleteBizCheckDetailByTitleId(bizCheckTitle.getId());
        insertBizCheckDetail(bizCheckTitle);
        return bizCheckTitleMapper.updateBizCheckTitle(bizCheckTitle);
    }

    /**
     * 批量删除检查清单
     * 
     * @param ids 需要删除的检查清单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBizCheckTitleByIds(String ids)
    {
        bizCheckTitleMapper.deleteBizCheckDetailByTitleIds(Convert.toStrArray(ids));
        return bizCheckTitleMapper.deleteBizCheckTitleByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除检查清单信息
     * 
     * @param id 检查清单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBizCheckTitleById(Long id)
    {
        bizCheckTitleMapper.deleteBizCheckDetailByTitleId(id);
        return bizCheckTitleMapper.deleteBizCheckTitleById(id);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param bizCheckTitle 检查清单对象
     */
    public void insertBizCheckDetail(BizCheckTitle bizCheckTitle)
    {
        List<BizCheckDetail> bizCheckDetailList = bizCheckTitle.getBizCheckDetailList();
        Long id = bizCheckTitle.getId();
        if (StringUtils.isNotNull(bizCheckDetailList))
        {
            List<BizCheckDetail> list = new ArrayList<BizCheckDetail>();
            for (BizCheckDetail bizCheckDetail : bizCheckDetailList)
            {
                bizCheckDetail.setTitleId(id);
                list.add(bizCheckDetail);
            }
            if (list.size() > 0)
            {
                bizCheckTitleMapper.batchBizCheckDetail(list);
            }
        }
    }
}
